package com.yswee.mallnavi.view.navi;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.yswee.framework.utils.ViewInject;
import com.yswee.framework.widget.clickedtab.ClickedTabBar;
import com.yswee.mallnavi.R;
import com.yswee.mallnavi.entity.info.CategoryEntity;

import java.util.ArrayList;


/***
 * TabBar
 */
public class TabBar extends ClickedTabBar {

    @ViewInject
    private LinearLayout llcontent;

    public TabBar(Context context) {
        super(context);
    }

    public TabBar(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.view_info_navi_tabbar;
    }

    @Override
    protected void onBindListener() {
        super.onBindListener();
    }

    @Override
    protected int[] getChildViewResIDs() {
        return new int[] {
        };
    }

    public void setDataArray(ArrayList<CategoryEntity> array) {
        llcontent.removeAllViews();
        if(array != null && array.size() > 0) {
            childviewList = new View[array.size()];
            for(int i = 0; i < array.size(); i++) {
                ImageView imgpicture = (ImageView) LayoutInflater.from(mContext).inflate(R.layout.view_info_navi_tabbar_item, null, false);
                imgpicture.setTag(array.get(i));
                int resId = array.get(i).getIconResId(false);
                if (resId != 0) {
                    imgpicture.setBackgroundResource(0);
                }
                imgpicture.setImageResource(resId);
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                        (int) mContext.getResources().getDimension(R.dimen.dp_40),
                        (int) mContext.getResources().getDimension(R.dimen.dp_40)
                );
                layoutParams.rightMargin = (int) mContext.getResources().getDimension(R.dimen.dp_10);
                layoutParams.bottomMargin = (int) mContext.getResources().getDimension(R.dimen.dp_10);
                llcontent.addView(imgpicture, layoutParams);
                childviewList[i] = imgpicture;
                if (imgpicture != null){
                    final int index = i;
                    imgpicture.setOnClickListener(new OnClickListener() {
                        public void onClick(View v) {
                            if(null != listener) {
                                listener.onTabChanged(index);
                            }
                            setTabIndex(index);
                        }
                    });
                }
            }
        }
    }

    @SuppressWarnings("deprecation")
    @Override
    protected void focusChildView(int index, View v) {
        ImageView imgpicture = (ImageView) v;
        CategoryEntity category = (CategoryEntity) imgpicture.getTag();
        int resId = category.getIconResId(true);
        if (resId != 0) {
            imgpicture.setBackgroundResource(0);
        }
        imgpicture.setImageResource(resId);
    }

    @SuppressWarnings("deprecation")
    @Override
    protected void unfocusChildView(int index, View v) {
        ImageView imgpicture = (ImageView) v;
        CategoryEntity category = (CategoryEntity) imgpicture.getTag();
        int resId = category.getIconResId(false);
        if (resId != 0) {
            imgpicture.setBackgroundResource(0);
        }
        imgpicture.setImageResource(resId);
    }

}